<?php

namespace App\Admin\Controllers;

use App\Models\Wallet;
use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class WalletController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户钱包')
            ->description('用户钱包')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Wallet);

        $grid->id('Id');
        $grid->user_id('用户昵称')->display(function($value){
            $user = User::where('id',$value)->value('nickname');
            if($user){
                return $user;
            }
        });
        $grid->money('余额')->display(function($value){
            return "<span class='label label-primary'>{$value}</span>";
        });
        $grid->tmc('代币余额')->display(function($value){
            return "<span class='label label-primary'>{$value}</span>";
        });
        $grid->computing_power('算力')->display(function($value){
            return "<span class='label label-primary'>{$value}</span>";
        });

        // $grid->disableActions();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableCreateButton();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Wallet::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->money('Money');
        $show->tmc('Tmc');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Wallet);

        $form->number('user_id', '用户id');
        $form->decimal('money', '余额')->default(0.00);
        $form->decimal('tmc', 'E格码')->default(0.0000);
        $form->decimal('computing_power', '算力')->default(0.0000);

        return $form;
    }
}
